package com.guodefu.demo.controller;

import com.guodefu.demo.domain.Payment;
import com.guodefu.demo.service.PaymentService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/api")
public class MyController {

    private final PaymentService paymentService;

    public MyController(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    @PostMapping(value = "/referer")
    public List<Payment> getReferer(HttpServletRequest request) {
        // javax.servlet.http.HttpServletRequest 在 Spring Boot 3.3.1 中无法识别
        // 将其中的 javax 替换为 jakarta

//        Payment payment = paymentService.selectByAmount(new BigDecimal("234"));
//        log.info("对象1：{}", payment);
//        payment = paymentService.getById(payment.getId());
//        log.info("对象2：{}", payment);
//
//        payment.setId(null);
//        paymentService.save(payment);
//        // insert执行后payment.id会被填充
        List<Payment> list = paymentService.list();
//        log.info("对象3：{}", list);
//
//        payment.setPayName("斯蒂芬");
//        paymentService.saveOrUpdate(payment);
//        list = paymentService.list();
//        log.info("对象4：{}", list);
//
//        list.forEach(s -> s.setAmount(new BigDecimal("999.99")));
//        paymentService.saveOrUpdateBatch(list);
//        list = paymentService.list();
//        log.info("对象5：{}", list);

        // 从 HttpServletRequest 中获取 Referer 请求头
        // Referer 头将包含发起请求页面的完整 URL
        // Origin 头将包含发起请求页面的基本 URL
//        String referer = request.getHeader("Referer");
        return list;
    }
}

